home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group97a.txt / 000033_icon-group-sender _Mon Feb 3 10:56:44 1997.msg < prev    next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Received: by cheltenham.cs.arizona.edu; Mon, 3 Feb 1997 13:13:43 MST
  2. Date: Mon, 3 Feb 1997 10:56:44 -0800
  3. Message-Id: <199702031856.KAA21895@dfw-ix3.ix.netcom.com>
  4. X-Sender: bobalex@popd.ix.netcom.com
  5. X-Mailer: Windows Eudora Pro Version 2.1.2
  6. Mime-Version: 1.0
  7. Content-Type: text/plain; charset="us-ascii"
  8. To: Junrong Yuan <joan@cs.ucr.edu>, Icon Group <icon-group@cs.arizona.edu>
  9. From: Bob Alexander <bobalex@ix.netcom.com>
  10. Subject: Re: Icon Tables
  11. Errors-To: icon-group-errors@cs.arizona.edu
  12. Status: RO
  13. Content-Length: 2588
  14.  
  15. At 03:06 PM 2/2/97 -0800, Junrong Yuan wrote:
  16.  
  17. >    I have a question about Icon data structure of 'table'. As I
  18. >understand it, a table is a 2D array that has a 'key', and a 'value'
  19.  
  20. That's one way of looking at tables.  However, I prefer to think of them as
  21. one-dimensional "arrays", with elements indexed by *any* datatype rather
  22. than by an integer.  The table syntax in Icon supports that mental image by
  23. using a "subscripting" notation (myTable["abc"]).
  24.  
  25. >I wonder what kind of types that the  'value' field support. To be more
  26. >specific, does it support 'record' or 'list'?
  27.  
  28. The values in tables can be of any Icon datatype, including records, lists,
  29. tables, files, what-have-you.  For that matter, keys can be of any datatype,
  30. too.  Strings are probably the most commonly-used, but keys can also be
  31. integers, files, etc.  Read carefully the rules about the "===" comparison
  32. operator to see exactly how keys are determined to be "equal" for various
  33. datatypes (for example, lists are "===" only if they are the *same* list,
  34. not just a similar list).
  35.  
  36.  
  37. >The Icon book
  38. >said it can be used to implement symbol tables, etc. In the case of symbol
  39. >table, a key may correspond to several attributes,  I am thinking of
  40. >putting all the attributes in a record, but I couldn't find a way to store
  41. >a record as 'value' filed in a table. 
  42.  
  43. Here's how to do that:
  44.  
  45.   - Declare the record.  This must be outside of any procedure.
  46.  
  47.         record SymTableEntry(field1,field2)  # declare the record
  48.  
  49.   - Create the table.  This must be in a procedure, and should be executed
  50.     only once.  Note that you might want your symbol table to be global, so
  51.     it should be declared as such.
  52.  
  53.        global SymTable  # this declaration must be outside of any procedure
  54.        ...
  55.        SymTable := table()  # this must be in a procedure
  56.  
  57.   - Here is the code to create an entry record and add it to the table
  58.  
  59.        symbol := "abc"
  60.        SymTable[symbol] := SymTableEntry(123,456)
  61.  
  62.  
  63. >    Another question: does list support search functions? If I have 
  64. >a list of records, and I want to find a given item in the list that has a
  65. >given value in a certain record field. What shall  I do ?
  66.  
  67. There is no list search function in Icon.  However, such things can be coded
  68. so succinctly in Icon that they really aren't as useful as in some other
  69. languages.  Here is one compact way to do it:
  70.  
  71.       if (foundEntry := !myList).aField == "xyz" then
  72.           <deal with found record, "foundEntry">
  73.       else
  74.           <deal with no such element in list>
  75.  
  76.  
  77. Hope this helps.
  78.  
  79. -- Bob Alexander
  80. bobalex@ix.netcom.com
  81.  
  82.